{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 多维数组",
   "id": "302fd56688f12fe9"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:06:11.758074Z",
     "start_time": "2025-08-27T06:06:11.678916Z"
    }
   },
   "cell_type": "code",
   "source": "import numpy as np",
   "id": "f0d36d61c59c05f",
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1.1 数组维度操作",
   "id": "d14d7920b5b40cd4"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.1.1 视图变维（数据共享）：\n",
    "- a.reshape(new_shape)：返回一个新视图（数据共享），不改变原数组。新形状必须与元素总数兼容。\n",
    "- a.ravel()：将数组展平为一维（返回视图）。\n",
    "![内存图](image/Numpy内存图.png)"
   ],
   "id": "dcf393d8bc4440c7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:30:38.139506Z",
     "start_time": "2025-08-27T06:30:38.127536Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\"\"\"\n",
    "视图变维\n",
    "\"\"\"\n",
    "arr1 = np.arange(1,9)\n",
    "print(arr1) # [1 2 3 4 5 6 7 8 9]\n",
    "print('arr1的地址',id(arr1))\n",
    "print('=' * 30)\n",
    "# 变维 （2,4）\n",
    "arr2 = arr1.reshape(2,4) # 2行4列\n",
    "print(arr1)\n",
    "print('=' * 30)\n",
    "print(arr2)\n",
    "print(arr2.shape)\n",
    "print('arr2的地址',id(arr2))\n",
    "\n",
    "print('=' * 30)\n",
    "# 变维(2,2,2)\n",
    "arr3 = arr1.reshape(2,2,2)\n",
    "print(arr3)\n",
    "print(arr3.shape)\n",
    "print(arr3.ndim)\n",
    "print('arr3的地址是:',id(arr3))\n",
    "print('=' * 30)\n",
    "\n",
    "print('$' * 30)\n",
    "# 将多维变一维\n",
    "arr4 = arr3.ravel()\n",
    "print(arr4)\n",
    "print(arr4.shape)\n",
    "print(arr4.ndim)\n",
    "print('arr4的地址是:',id(arr4))\n",
    "\n",
    "print('&' * 30)\n",
    "\"\"\"\n",
    "这里是共享数据的，但是arr1 和arr2,arr3,arr4地址是不一样的，\n",
    "这是因为，在NumPy数组由两个主要部分组成：\n",
    "    数据缓冲区（Data buffer） - 存储实际数据值的内存块  1 2 3 4 5 6 7 8 9\n",
    "    数组元数据（Metadata） - 描述如何解释数据的信息（形状、维度、步长等）\n",
    "\"\"\"\n",
    "# 检查数据是否共享\n",
    "print('共享数据:', np.shares_memory(arr1, arr2))\n",
    "print('共享数据:', np.shares_memory(arr2, arr3))\n",
    "print('共享数据:', np.shares_memory(arr2, arr4))\n"
   ],
   "id": "9c5c965c09fd37e6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8]\n",
      "arr1的地址 1539125397808\n",
      "==============================\n",
      "[1 2 3 4 5 6 7 8]\n",
      "==============================\n",
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n",
      "(2, 4)\n",
      "arr2的地址 1539125409040\n",
      "==============================\n",
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "(2, 2, 2)\n",
      "3\n",
      "arr3的地址是: 1539125407888\n",
      "==============================\n",
      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",
      "[1 2 3 4 5 6 7 8]\n",
      "(8,)\n",
      "1\n",
      "arr4的地址是: 1539125407024\n",
      "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n",
      "共享数据: True\n",
      "共享数据: True\n",
      "共享数据: True\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.1.2. 复制变维（数据独立）：flatten()\n",
    "- a.flatten()：返回展平后的新数组（数据独立复制）。\n"
   ],
   "id": "ba42a1a7945919aa"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:43:31.126305Z",
     "start_time": "2025-08-27T06:43:31.120110Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(arr3) # 三维\n",
    "print('arr3的地址是:',id(arr3))\n",
    "print('=' * 30)\n",
    "# 将三维拉成一维\n",
    "arr5 = arr3.flatten()\n",
    "print(arr5)\n",
    "print(arr5.shape)\n",
    "print(arr5.ndim)\n",
    "print('arr4的地址是:',id(arr5))\n",
    "\n",
    "\"\"\"\n",
    "用 flatten() 会复制一份数据，数据独立\n",
    "\"\"\"\n",
    "print('是否共享数据：',np.shares_memory(arr5, arr3))  # False"
   ],
   "id": "106a7864cbc4ad2e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "arr3的地址是: 1539125407888\n",
      "==============================\n",
      "[1 2 3 4 5 6 7 8]\n",
      "(8,)\n",
      "1\n",
      "arr4的地址是: 1539125408656\n",
      "是否共享数据： False\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "### 1.1.3. 就地变维：直接改变原数组对象的维度，不返回新数组\n",
    "- a.shape = new_shape：直接修改数组的 shape 属性。\n",
    "- a.resize(new_shape)：直接修改数组形状，如果新形状更大，会用0填充；更小则会截断数据。\n"
   ],
   "id": "c75f55b77bd4d3bd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-08-27T06:49:29.894891Z",
     "start_time": "2025-08-27T06:49:29.885351Z"
    }
   },
   "cell_type": "code",
   "source": [
    "arr6 = np.arange(1,9)\n",
    "print(arr6)\n",
    "print(arr6.shape)\n",
    "\n",
    "print('=' * 30)\n",
    "arr6.shape = (2,4)\n",
    "print(arr6)\n",
    "print(arr6.shape)\n",
    "\n",
    "print('$' * 30)\n",
    "arr6.resize(2,2,2)\n",
    "print(arr6)\n",
    "print(arr6.shape)"
   ],
   "id": "5666da4bb60eb326",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6 7 8]\n",
      "(8,)\n",
      "==============================\n",
      "[[1 2 3 4]\n",
      " [5 6 7 8]]\n",
      "(2, 4)\n",
      "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n",
      "[[[1 2]\n",
      "  [3 4]]\n",
      "\n",
      " [[5 6]\n",
      "  [7 8]]]\n",
      "(2, 2, 2)\n"
     ]
    }
   ],
   "execution_count": 33
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
